package com.gama.keyi;

import android.app.*;
import com.unity3d.player.*;
import android.provider.*;
import com.wb.jar.*;
import java.util.*;
import android.graphics.*;
import java.io.*;
import android.content.pm.*;
import android.net.*;
import android.os.*;
import android.content.res.*;
import xdown.*;
import android.util.*;
import android.content.*;
import android.view.*;

public class UnityPlayerActivity extends Activity
{
    public static Thread xDownThread;
    private BitmapListener bitmapListener;
    protected UnityPlayer mUnityPlayer;

    public UnityPlayerActivity() {
        this.bitmapListener = null;
    }

    public boolean dispatchKeyEvent(final KeyEvent keyEvent) {
        if (keyEvent.getAction() == 2) {
            return this.mUnityPlayer.injectEvent((InputEvent)keyEvent);
        }
        return super.dispatchKeyEvent(keyEvent);
    }

    protected void onActivityResult(final int n, final int n2, final Intent intent) {
        super.onActivityResult(n, n2, intent);
        if (n != 1) {
            return;
        }
        if (this.bitmapListener == null) {
            return;
        }
        if (n2 == -1) {
            final Uri data = intent.getData();
            Bitmap bitmap = null;
            Label_0082: {
                Label_0080: {
                    if (data != null) {
                        try {
                            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data);
                            break Label_0082;
                        }
                        catch (Exception ex) {
                            ex.printStackTrace();
                            break Label_0080;
                        }
                    }
                    final Bundle extras = intent.getExtras();
                    if (extras != null) {
                        bitmap = (Bitmap)extras.getParcelable("data");
                        break Label_0082;
                    }
                }
                bitmap = null;
            }
            if (bitmap != null) {
                try {
                    final String format = String.format("%s/Android/data/%s/files/%d.png", AndroidUtil.EXTERNAL_STORAGE_DIR, this.getPackageManager().getPackageInfo(this.getApplicationContext().getPackageName(), 0).packageName, new Date().getTime());
                    try {
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, (OutputStream)new FileOutputStream(format));
                    }
                    catch (FileNotFoundException ex2) {
                        ex2.printStackTrace();
                    }
                    this.bitmapListener.onBitmap(format);
                }
                catch (PackageManager.NameNotFoundException ex3) {
                    ex3.printStackTrace();
                    this.bitmapListener.onBitmap(null);
                }
            }
            else {
                this.bitmapListener.onBitmap(null);
            }
        }
        this.bitmapListener.onBitmap(null);
    }

    public void onConfigurationChanged(final Configuration configuration) {
        super.onConfigurationChanged(configuration);
        this.mUnityPlayer.configurationChanged(configuration);
    }

    protected void onCreate(final Bundle bundle) {
        this.requestWindowFeature(1);
        super.onCreate(bundle);
        this.getWindow().setFormat(2);
        (UnityPlayerActivity.xDownThread = new Thread() {
            @Override
            public void run() {
                try {
                    while (true) {
                        final boolean onLibInit = Xdown.onLibInit("tcp://127.0.0.1:2012/10.25.96.3:33320", "127.0.0.1:2766", "127.0.0.1:2013", "", "", "", "", "", "", "");
                        final StringBuilder sb = new StringBuilder();
                        sb.append("initOK : ");
                        sb.append(onLibInit);
                        Log.e("Unity", sb.toString());
                        if (onLibInit) {
                            final String onLibListen = Xdown.onLibListen();
                            final StringBuilder sb2 = new StringBuilder();
                            sb2.append("onLibListen : ");
                            sb2.append(onLibListen);
                            Log.e("Unity", sb2.toString());
                        }
                        else {
                            Thread.sleep(500L);
                        }
                    }
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
        this.setContentView((View)(this.mUnityPlayer = new UnityPlayer((ContextWrapper)this)));
        this.mUnityPlayer.requestFocus();
    }

    protected void onDestroy() {
        this.mUnityPlayer.quit();
        super.onDestroy();
        final Thread xDownThread = UnityPlayerActivity.xDownThread;
        if (xDownThread != null) {
            xDownThread.stop();
        }
    }

    public boolean onGenericMotionEvent(final MotionEvent motionEvent) {
        return this.mUnityPlayer.injectEvent((InputEvent)motionEvent);
    }

    public boolean onKeyDown(final int n, final KeyEvent keyEvent) {
        return this.mUnityPlayer.injectEvent((InputEvent)keyEvent);
    }

    public boolean onKeyUp(final int n, final KeyEvent keyEvent) {
        return this.mUnityPlayer.injectEvent((InputEvent)keyEvent);
    }

    public void onLowMemory() {
        super.onLowMemory();
        this.mUnityPlayer.lowMemory();
    }

    protected void onNewIntent(final Intent intent) {
        this.setIntent(intent);
    }

    protected void onPause() {
        super.onPause();
        this.mUnityPlayer.pause();
    }

    protected void onResume() {
        super.onResume();
        this.mUnityPlayer.resume();
    }

    public boolean onTouchEvent(final MotionEvent motionEvent) {
        return this.mUnityPlayer.injectEvent((InputEvent)motionEvent);
    }

    public void onTrimMemory(final int n) {
        super.onTrimMemory(n);
        if (n == 15) {
            this.mUnityPlayer.lowMemory();
        }
    }

    public void onWindowFocusChanged(final boolean b) {
        super.onWindowFocusChanged(b);
        this.mUnityPlayer.windowFocusChanged(b);
    }

    public void setBitmapListener(final BitmapListener bitmapListener) {
        if (this.bitmapListener != null) {
            this.bitmapListener = null;
        }
        this.bitmapListener = bitmapListener;
    }
}
